<template>
  <!-- 企业等级 -->
  <div class="body_all">
    <div class="ba_top">
      <div style="margin-top: 8px">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="showEditer(0)"
          >新增等级</el-button
        >
      </div>
    </div>
    <div class="ba_main">
      <el-table
      v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        height="250"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column prop="title" label="等级名称" fixed> </el-table-column>
        <el-table-column prop="sort" label="序号" width="100">
        </el-table-column>
        <el-table-column label="操作" width="100" flex="right">
          <template slot-scope="scope">
            <span class="edit_btn" @click="showEditer(scope.row)">编辑</span>
            <span
              class="edit_btn"
              style="margin-left: 10px"
              @click="delFrom(scope.row.id)"
              >删除</span
            >
          </template>
        </el-table-column>
      </el-table>
      <div v-if="false" class="footerPage">
        <i class="el-icon-arrow-left"></i>
        <i class="el-icon-arrow-right"></i>
      </div>
    </div>
    <!-- 新增编辑 -->
    <el-dialog
      :title="titleEditer"
      :close-on-click-modal="false"
      :visible.sync="dialogEditer"
      width="45%"
      :close-on-press-escape="false"
    >
      <div class="dialogDiv">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="等级名称" prop="title">
            <el-input
              v-model="ruleForm.title"
              placeholder="请输入等级名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="序号" prop="sort">
            <el-input
              v-model="ruleForm.sort"
              placeholder="请输入序号"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="foot_btn">
        <el-button @click="resetForm('ruleForm')">取消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      titleEditer: "",
      dialogEditer: false,
      // 搜索条件
      searchList: {
        name: "",
        componeName: "",
      },
      phoneSelList: [
        {
          value: 0,
          label: "手机",
        },
      ],
      ruleForm: {
        id: "",
        title: "",
        sort: 1,
      },
      rules: {
        title: [{ required: true, message: "请输入等级名称", trigger: "blur" }],
        sort: [{ required: true, message: "请输入序号", trigger: "blur" }],
      },
      tableData: [],
    };
  },
  created() {
    this.loading = true;
    this.getData();
  },
  methods: {
    // 获取数据列表
    getData() {
      let that = this;
      this.$cmsapi.customerLevelList().then(
        (res) => {
          if (res.code == 0) {
            that.tableData = res.data;
          } else {
            that.$message.error(res.msg);
          }
          that.loading = false;
        },
        (err) => {
          console.log(err);
          that.loading = false;
        }
      );
    },
    // 条件重置
    btnRefresh() {
      this.searchList = this.$options.data().searchList;
    },
    // 新增编辑
    showEditer(type) {
      if (type == 0) {
        this.titleEditer = "新增等级";
      } else {
        this.titleEditer = "编辑等级";
        this.ruleForm = JSON.parse(JSON.stringify(type));
      }
      this.dialogEditer = true;
    },
    // 提交
    submitForm(formName) {
      let that = this;
      that.loading = true;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$cmsapi.customerLevelSave(that.ruleForm).then(
            (res) => {
              if (res.code == 0) {
                this.$message({
                  message: that.ruleForm.id == "" ? "新增成功" : "编辑成功",
                  type: "success",
                });
                that.dialogEditer = false;
                that.ruleForm = that.$options.data().ruleForm;
                that.getData();
              } else {
                that.$message.error(res.msg);
                that.loading = false;
              }
            },
            (err) => {
              console.log(err);
            }
          );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 重置&取消
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogEditer = false;
      this.ruleForm = this.$options.data().ruleForm;
    },
    // 删除
    delFrom(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          this.$cmsapi.customerLevelDel(id).then(
            (res) => {
              if (res.code == 0) {
                this.$message({
                  message: "删除成功",
                  type: "success",
                });
                this.getData();
              } else {
                this.$message.error(res.msg);
                this.loading = false;
              }
            },
            (err) => {
              console.log(err);
              this.loading = false;
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>